<template>
  <a-modal :title="modal.title" :visible="modal.visible" maskClosable width="900px" @cancel="$emit('done')"
    @ok="submit">
    <a-form :form="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
      <a-form-item label="id" style="display: none;">
        <a-input v-decorator="['id']" />
      </a-form-item>

      <a-form-item label="productId" style="display: none;">
        <a-input v-decorator="['productId']" />
      </a-form-item>
      <a-form-item label="indemnifyId" style="display: none;">
        <a-input v-decorator="['indemnifyId']" />
      </a-form-item>

      <a-form-item label="保险责任">
        <a-select mode="multiple" v-decorator="['dutyIds']">
          <a-select-option v-for="item in dutyList" :value="item.id">
          {{ item.isMust === 1 ? '【必选】' : '【可选】' }}{{ item.dutyName }}（{{ item.recordName }}）
          </a-select-option>
        </a-select>
      </a-form-item>

    </a-form>
  </a-modal>
</template>

<script>
  import { add, update, listAllForSelect } from '@/api/aibao/product/serious'
  import { listProductForSelect } from '@/api/aibao/product/product'
  import { listProductIndemnifyForSelect } from '@/api/aibao/product/indemnify'
  import { listCompanyForSelect } from '@/api/company'
  import { listByPage, addBatch } from '@/api/aibao/product/seriousIndemnify'

  import pick from 'lodash.pick'
  const fields = ['id', 'productId', 'indemnifyId', 'sortOrder', 'dutyIds']

  export default {
    props: {
      modal: {
        type: Object,
        required: true
      },
    },
    data() {
      return {
        queryParam: {
          productId: ''
        },
        form: this.$form.createForm(this),
        companys: [], //保司列表
        indemnifies: [], //保障期列表
        products: [], //产品列表
        dutyList: [], //保险责任：多选
      }
    },
    created() {
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.$watch('modal', () => {
        if (this.modal) {
          this.form.setFieldsValue(pick(this.modal.form, fields))
          // 加载列表，2025年4月9日10:57:11
          this.listAllForSelect()
        }
      }, { deep: true })
    },
    methods: {
      listAllForSelect(){
        if(this.modal.form.productId){
          this.queryParam.productId = this.modal.form.productId
          listAllForSelect(this.queryParam).then(res => {
            this.dutyList = res.data
          }).catch(() => {})
        }
      },
      submit() {
        this.form.validateFields((errors, values) => {
          if (!errors) {
            let params = { ...values }
            if(params.dutyIds && params.dutyIds.length > 0){
              addBatch(values).then(res => {
                  this.$message.success('操作成功')
                  this.$emit('done')
              }).catch(() => {})
            }else {
                this.$message.error('保险责任不能为空')
                return;
            }

          }
        })
      },
    },

  }
</script>